package com.ly.cloud.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.commom.constant.StateConstant;
import com.ly.cloud.dto.GradeProfessionalInfoDto;
import com.ly.cloud.entity.GradeProfessionalInfoPo;
import com.ly.cloud.entity.ProfessionalInfoPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.mapper.GradeProfessionalInfoMapper;
import com.ly.cloud.mapper.ProfessionalInfoMapper;
import com.ly.cloud.service.GradeProfessionalInfoService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 年级专业
 * @Author: pengshiqng
 * @Date: Create in 2018/12/14
 */
@Service
public class GradeProfessionalInfoServiceImpl implements GradeProfessionalInfoService {

    @Autowired
    private GradeProfessionalInfoMapper gradeProfessionalInfoPoMapper;

    @Autowired
    private ProfessionalInfoMapper professionalInfoPoMapper;

    /**
     * 分页+条件查询
     */
    @Override
    public Page<GradeProfessionalInfoVo> selectPage(Page<GradeProfessionalInfoVo> page, GradeProfessionalInfoDto dto) {
        try {
            return page.setRecords(gradeProfessionalInfoPoMapper.selectPage(page, dto));
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * 查询所有的年级
     */
    @Override
    public List<String> findAllGrade() {
        try {
            return gradeProfessionalInfoPoMapper.findAllGrade();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * 根据年级查询所有的学院
     */
    @Override
    public List<DwSelectVO> findAllCollegeByGrade(String grade) {
        try {
            return gradeProfessionalInfoPoMapper.findAllCollegeByGrade(grade);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * 删除
     */
    @Override
    public int deleteById(String id) {
        try {
            return gradeProfessionalInfoPoMapper.deleteById(id) ;
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("删除失败!", e);
        }
    }

    /**
     * 修改
     */
    @Override
    public int update(String loginUserName , GradeProfessionalInfoDto dto) {
        try {
            //把dto对象转换成po对象
            GradeProfessionalInfoPo po = BeanCopyUtils.copyBeanProperties(dto, GradeProfessionalInfoPo.class);
            po.setXgr(loginUserName);
            po.setXgsj(new Date());
            return gradeProfessionalInfoPoMapper.updateById(po) ;
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("删除失败!", e);
        }
    }

    /**
     * 批量删除
     */
    @Override
    public int deleteByIds(List<String> ids) {
        try {
            return gradeProfessionalInfoPoMapper.deleteBatchIds(ids) ;
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("批量删除失败!", e);
        }
    }

    /**
     * 查询所有校区校区
     */
    @Override
    public List<CampusSelectVo> findAllCampus() {
        try {
            return gradeProfessionalInfoPoMapper.findAllCampus();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
     * 设置校区
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCampus(String loginUserName, List<String> ids, String campus) {
        try {
            GradeProfessionalInfoPo po = new GradeProfessionalInfoPo() ;
            po.setCjr(loginUserName);
            po.setCjsj(new Date());
            for (String id : ids ) {
                po.setNjzydm(id);
                po.setSsxq(campus);
                gradeProfessionalInfoPoMapper.updateById(po);
            }
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("设置校区失败!", e);
        }
    }


    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(String loginUserName ,List<String> ids, String grade, String campus) {
        try {
            //计数，每新增成功一条数据后count加1
            int count = 0 ;
            GradeProfessionalInfoPo po = new GradeProfessionalInfoPo() ;
            po.setSsxq(campus);
            po.setNj(grade);
            po.setCjr(loginUserName);
            po.setCjsj(new Date());

            for (String id : ids){
                //查询专业对应的学制
                ProfessionalInfoPo professionalInfoPo = professionalInfoPoMapper.selectById(id);
                String xz = professionalInfoPo.getXz();
                // 年级专业信息主键 = 年级 + 学制 + 专业信息主键
                String njzydm = grade + xz + id ;
                //查询年级专业信息表中是否存在记录，如果不存在，新增，存在不新增
                GradeProfessionalInfoPo gradeProfessionalInfoPo = gradeProfessionalInfoPoMapper.selectById(njzydm);
                // gradeProfessionalInfoPo 为空，年级专业信息表中无数据
                if (gradeProfessionalInfoPo == null){
                    po.setNjzydm(njzydm);
                    // 年级专业信息名称 = 年级  + 专业信息名称
                    po.setNjzymc(grade + professionalInfoPo.getZymc());
                    po.setZydm(professionalInfoPo.getZyh());
                    po.setZymc(professionalInfoPo.getZymc());
                    po.setXwdm(professionalInfoPo.getXw());
                    po.setZylx(professionalInfoPo.getZylb());
                    po.setSsxy(professionalInfoPo.getLssj());
                    po.setYwmc(professionalInfoPo.getYwmc());
                    po.setPyccdm(professionalInfoPo.getCc());
                    po.setXz(professionalInfoPo.getXz());
                    // 1:启用
                    po.setSfqy(StateConstant.UP);
                    gradeProfessionalInfoPoMapper.insert(po);
                    count ++ ;
                }
            }
            return count;
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("新增失败!", e);
        }
    }

    /**
     * 复制
     */
    @Override
    public int copy(String loginUserName , List<String> ids, String targetGrade) {
        //计数，每新增成功一条数据后count加1
        int count = 0 ;
        for (String id : ids) {
            //查询数据库
            GradeProfessionalInfoPo po = gradeProfessionalInfoPoMapper.selectById(id);
            String newId = targetGrade + id.substring(Math.min(4, id.length()),id.length()) ;
            GradeProfessionalInfoPo newPo = gradeProfessionalInfoPoMapper.selectById(newId);
            //如果不存在，则新增
            if (newPo == null){
                po.setNjzydm(newId);
                String njzymc = po.getNjzymc() ;
                po.setNjzymc( targetGrade + njzymc.substring(Math.min(4, njzymc.length()),njzymc.length()));
                po.setCjr(loginUserName);
                po.setCjsj(new Date());
                gradeProfessionalInfoPoMapper.insert(po) ;
                count++ ;
            }
        }
        return count ;
    }

    /** 
   	 * Method Name: selectNjZyByNj
   	 * Description:  
   	 * 根据年级查询年级专业，下拉列表
   	 * @param nj 年级
   	 * @return 年级专业列表
   	 * @author luhuacong
   	 * @date: 2018年12月20日
   	 */
	@Override
	public List<NjzySelectVo> selectNjZyByNj(String nj) {
		List<NjzySelectVo> voList = null;
		try {
			voList = gradeProfessionalInfoPoMapper.selectNjZyByNj(nj);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询年级专业失败!", e);
		}
		return voList;
	}

    /**
     * @Description: 根据年级、学院查询年级专业
     * @Author: pengshiqing
     * @CreateDate: 2018/12/26
     */
    @Override
    public List<NjzySelectVo> findNjzyByNjAndXy(String nj, String xy) {
        try {
            return gradeProfessionalInfoPoMapper.findNjzyByNjAndXy(nj, xy);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }

    /**
    * @Description: 通过年级专业代码查询年级专业信息
    * @author: pengshiqing
    * @date: 2019/1/7
    */
    @Override
    public GradeProfessionalInfoVo findByNjzydm(String njzydm) {
        try {
            GradeProfessionalInfoPo po = gradeProfessionalInfoPoMapper.selectById(njzydm);
            //把po对象转换成vo对象 返回
            return BeanCopyUtils.copyBeanProperties(po,GradeProfessionalInfoVo.class);
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
    }


}
